/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
//package MuleGame;
package MuleGUI;

import javax.swing.*;

import MuleBackend.*;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Dimension;

import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;

import java.awt.Font;
import java.awt.Scrollbar;
import java.awt.Canvas;
import java.awt.Panel;
import java.awt.ScrollPane;
import java.awt.Color;
import java.awt.TextField;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * This GUI will take in number of players, difficulty
 * level, map type, and number of rounds for multiplayer network gameplay.
 * These information will be sent to Game class
 * where they will be stored inside their respected
 * variable names
 * This GUI display when the Network->Host menu is 
 * clicked in board GUIs.
 * This GUI will open MultiplayerGUI when the OK button is 
 * clicked.
 */
public class MultiplayerGUI extends javax.swing.JFrame {
	int playersconnected = 0;

    /**
     * Creates new form StartGUI
     * @throws UnknownHostException 
     */
    public MultiplayerGUI() throws UnknownHostException {
    	setResizable(false);
        initComponents();
    }
    
    public String getDiff(String difStr){
    	if(difStr.equals("Easy"))			
    		return "Easy";
		else if(difStr.equals("Medium"))
			return "Medium";
		else
			return "Difficult";
    }


    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     * @throws UnknownHostException 
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() throws UnknownHostException {

        playersLabel = new javax.swing.JLabel();
        difficultyLabel = new javax.swing.JLabel();
        difficultyComboBox = new javax.swing.JComboBox();
        startButton = new javax.swing.JButton();
        startButton.addMouseListener(new MouseAdapter() {
        	@Override
        	public void mouseClicked(MouseEvent e) 
        	{
        		if(playersconnected == NameGUI.numPlayer)
        		{
        			String difficulty = getDiff((String)difficultyComboBox.getSelectedItem());
           		 	String mapNum = (String)mapComboBox.getSelectedItem();
           		 	int rounds = Integer.parseInt((String)roundComboBox.getSelectedItem());
           		 	dispose();
           		 	
        		 	GameDriver.game.setDifficulty(difficulty);
        		 	if (mapNum.equals("Normal")){
        		 		GameDriver.game.setMapNum(1);
            		 	GameDriver.game.generateMap(1);
        		 	}
        		 	else{
        		 		GameDriver.game.setMapNum(2);
            		 	GameDriver.game.generateMap(2);
        		 	}
        		 	GameDriver.game.setRounds(rounds);
        		
	        		GameDriver.game.createMap();
					GameDriver.game.startStage();
        		}
       		 		
        		if(playersconnected != NameGUI.numPlayer)
        			JOptionPane.showMessageDialog(null,"There aren't enough players connected.");
        	}
        });
        cancelButton = new javax.swing.JButton();
        mapLabel = new javax.swing.JLabel();
        mapComboBox = new javax.swing.JComboBox();
        roundLabel = new javax.swing.JLabel();
        roundComboBox = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Network UI (Multiplayer GUI)");

        playersLabel.setFont(new Font("Times New Roman", Font.PLAIN, 16)); // NOI18N
        playersLabel.setText("Players");

        difficultyLabel.setFont(new Font("Times New Roman", Font.PLAIN, 16)); // NOI18N
        difficultyLabel.setText("Difficulty");

        difficultyComboBox.setFont(new Font("Tahoma", Font.PLAIN, 10)); // NOI18N
        difficultyComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Easy", "Medium", "Difficult" }));

        startButton.setFont(new Font("Tahoma", Font.PLAIN, 17)); // NOI18N
        startButton.setText("Start Game");

        cancelButton.setFont(new Font("Tahoma", Font.PLAIN, 16)); // NOI18N
        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        mapLabel.setFont(new Font("Times New Roman", Font.PLAIN, 16)); // NOI18N
        mapLabel.setText("Map");

        mapComboBox.setFont(new Font("Tahoma", Font.PLAIN, 10)); // NOI18N
        mapComboBox.setModel(new DefaultComboBoxModel(new String[] {"Normal", "Random"}));

        roundLabel.setFont(new Font("Times New Roman", Font.PLAIN, 16)); // NOI18N
        roundLabel.setText("Rounds");

        roundComboBox.setFont(new Font("Tahoma", Font.PLAIN, 10)); // NOI18N
        roundComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "6", "8", "10", "12" }));
        
        lblHostIpAddress = new JLabel();
        lblHostIpAddress.setText("Host IP Address:");
        lblHostIpAddress.setFont(new Font("Times New Roman", Font.PLAIN, 14));
        
        textArea = new JTextArea();
        textArea.setEditable(false);
        textArea.setWrapStyleWord(true);
        textArea.setLineWrap(true);
        textArea.setDropMode(DropMode.INSERT);
        
        hostIPLabel = new JLabel();
        hostIPLabel.setText(InetAddress.getLocalHost().getHostAddress());
        
        tglbtnNewToggleButton = new JButton("Connect");
        tglbtnNewToggleButton.addMouseListener(new MouseAdapter() {
        	@Override
        	public void mouseClicked(MouseEvent e) 
        	{
        		if(playersconnected != NameGUI.numPlayer)
        		new NameGUI(NameGUI.numPlayer).setVisible(true);        			
        	}
        });
        
        lblNewLabel = new JLabel();
        lblNewLabel.setText(Integer.toString(NameGUI.numPlayer));
        
        final JTextArea textArea_1 = new JTextArea();
        
        btnNewButton = new JButton("Send");
        btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 18));
        btnNewButton.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent arg0) {
        		textArea.append(textArea_1.getText() + "\n");
        		textArea_1.removeAll();
        	}
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        layout.setHorizontalGroup(
        	layout.createParallelGroup(Alignment.LEADING)
        		.addGroup(layout.createSequentialGroup()
        			.addContainerGap()
        			.addGroup(layout.createParallelGroup(Alignment.LEADING)
        				.addGroup(layout.createSequentialGroup()
        					.addGroup(layout.createParallelGroup(Alignment.LEADING)
        						.addGroup(layout.createSequentialGroup()
        							.addComponent(roundLabel)
        							.addPreferredGap(ComponentPlacement.UNRELATED)
        							.addComponent(roundComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        						.addGroup(layout.createSequentialGroup()
        							.addComponent(playersLabel)
        							.addGap(18)
        							.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 6, GroupLayout.PREFERRED_SIZE)))
        					.addPreferredGap(ComponentPlacement.RELATED, 8, Short.MAX_VALUE)
        					.addGroup(layout.createParallelGroup(Alignment.TRAILING)
        						.addComponent(mapLabel)
        						.addComponent(difficultyLabel))
        					.addGap(18)
        					.addGroup(layout.createParallelGroup(Alignment.TRAILING, false)
        						.addComponent(mapComboBox, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        						.addComponent(difficultyComboBox, 0, 91, Short.MAX_VALUE))
        					.addPreferredGap(ComponentPlacement.UNRELATED)
        					.addGroup(layout.createParallelGroup(Alignment.LEADING)
        						.addComponent(startButton)
        						.addComponent(cancelButton, GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE)))
        				.addGroup(layout.createSequentialGroup()
        					.addGroup(layout.createParallelGroup(Alignment.TRAILING)
        						.addComponent(textArea_1, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 278, Short.MAX_VALUE)
        						.addComponent(textArea, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 278, Short.MAX_VALUE))
        					.addPreferredGap(ComponentPlacement.RELATED)
        					.addGroup(layout.createParallelGroup(Alignment.LEADING)
        						.addComponent(btnNewButton, GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE)
        						.addComponent(lblHostIpAddress, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE)
        						.addComponent(hostIPLabel, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE)
        						.addComponent(tglbtnNewToggleButton, GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE))))
        			.addContainerGap())
        );
        layout.setVerticalGroup(
        	layout.createParallelGroup(Alignment.LEADING)
        		.addGroup(layout.createSequentialGroup()
        			.addContainerGap()
        			.addGroup(layout.createParallelGroup(Alignment.LEADING)
        				.addComponent(textArea, GroupLayout.PREFERRED_SIZE, 248, GroupLayout.PREFERRED_SIZE)
        				.addGroup(layout.createSequentialGroup()
        					.addComponent(lblHostIpAddress, GroupLayout.PREFERRED_SIZE, 16, GroupLayout.PREFERRED_SIZE)
        					.addPreferredGap(ComponentPlacement.RELATED)
        					.addComponent(hostIPLabel)
        					.addGap(18)
        					.addComponent(tglbtnNewToggleButton)))
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addGroup(layout.createParallelGroup(Alignment.LEADING, false)
        				.addComponent(btnNewButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        				.addComponent(textArea_1, GroupLayout.DEFAULT_SIZE, 57, Short.MAX_VALUE))
        			.addPreferredGap(ComponentPlacement.RELATED, 9, Short.MAX_VALUE)
        			.addGroup(layout.createParallelGroup(Alignment.TRAILING)
        				.addGroup(layout.createSequentialGroup()
        					.addGroup(layout.createParallelGroup(Alignment.BASELINE)
        						.addComponent(difficultyComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        						.addComponent(difficultyLabel)
        						.addComponent(playersLabel)
        						.addComponent(lblNewLabel))
        					.addPreferredGap(ComponentPlacement.RELATED)
        					.addGroup(layout.createParallelGroup(Alignment.BASELINE)
        						.addComponent(roundLabel)
        						.addComponent(roundComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        						.addComponent(mapLabel)
        						.addComponent(mapComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
        				.addGroup(layout.createSequentialGroup()
        					.addComponent(startButton)
        					.addPreferredGap(ComponentPlacement.RELATED)
        					.addComponent(cancelButton)))
        			.addContainerGap())
        );
        getContentPane().setLayout(layout);

        mapLabel.getAccessibleContext().setAccessibleName("");

        setSize(new Dimension(413, 434));
        setLocationRelativeTo(null);
    }// </editor-fold>                        

    
    public static void setTextArea(String str)
    {
    	textArea.append(str);
    }
    
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
    	GameDriver.game.server = null;
        setVisible(false);
    }                                        

    // Variables declaration - do not modify                     
    private javax.swing.JButton startButton;
    private javax.swing.JButton cancelButton;
    private javax.swing.JComboBox difficultyComboBox;
    private javax.swing.JComboBox mapComboBox;
    private javax.swing.JComboBox roundComboBox;
    private javax.swing.JLabel playersLabel;
    private javax.swing.JLabel difficultyLabel;
    private javax.swing.JLabel mapLabel;
    private javax.swing.JLabel roundLabel;
    private JLabel lblHostIpAddress;
    private static JTextArea textArea;
    private JLabel hostIPLabel;
    private JButton tglbtnNewToggleButton;
    private JLabel lblNewLabel;
    private JButton btnNewButton;
}